'use strict';
const db = uniCloud.database();
const $ = db.command.aggregate;
const dbCmd = db.command;
exports.main = async (event, context) => {
	//event为客户端上传的参数
	const {
		user_id, //用户id
		article_id, //文字id
		content, //评论内容
		comment_id = '',
		reply_id = '',
		is_reply = false
	} = event
	//获取用户信息
	let user = await db.collection('user').doc(user_id).get()
	user = user.data[0]
	//获取当前文章信息
	const article = await db.collection('article').doc(article_id).get()
	// 获取文章下的所有评论
	const comments = article.data[0].comments
	//封装评论参数
	let commentObj = {
		comment_id: getID(5), //评论id
		comment_content: content, //内容
		create_time: new Date().getTime(), //时间
		is_reply: is_reply, //区分主回复还是子回复
		author: {
			author_id: user._id, //作者id
			author_name: user.author_name, //作者姓名
			avatar: user.avatar,
			professional: user.professional //专业
		},
		replys: []
	}
	//评论文章
	if (comment_id === '') {
		commentObj.replys = []
		commentObj = dbCmd.unshift(commentObj)
	} else {
		// 获取评论的索引
		let commentIndex = comments.findIndex(item => item.comment_id === comment_id)
		let commentAuthor = ''
		if (is_reply) {
			//子回复
			commentAuthor = comments[commentIndex].replys.find(item => item.comment_id === reply_id)
		} else {
			// 主回复
			//获取作者信息
			commentAuthor = comments.find(item => item.comment_id === comment_id)
		}
		commentAuthor == commentAuthor.author.author_name
		commentObj.to = commentAuthor
		// 更新回复信息
		commentObj = {
			[commentIndex]: {
				replys: dbCmd.unshift(commentObj)
			}
		}
	}
	await db.collection('article').doc(article_id).update({
		comments: commentObj
	})

	//返回数据给客户端
	return {
		code: 200,
		msg: '数据更新成功'
	}
};

function getID(length) {
	return Number(Math.random().toString().substr(3, length) + Date.now()).toString(36)
}
